package com.alianzamedica.view;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.objectsearch.web.tools.Converter;

import com.alianzamedica.businessobject.Module;
import com.alianzamedica.tools.ValidateTool;

/**
 * @author Carlos
 * 
 */
public class ModuleEditForm extends ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4296259654125616723L;

	private String id; // 'MODULE_ID', 'int(10) unsigned', 'NO', 'PRI', '',
						// 'auto_increment'
	private String name; // 'MODULE_NAME', 'varchar(45)', 'NO', '', '', ''
	private String url; // 'MODULE_URL', 'varchar(200)', 'NO', '', '', ''

	/**
	 * id.
	 * 
	 * @return id.
	 */
	public String getId() {
		return id;
	}

	/**
	 * id.
	 * 
	 * @param id
	 *            id.
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * nombre.
	 * 
	 * @return nombre.
	 */
	public String getName() {
		return name;
	}

	/**
	 * nombre.
	 * 
	 * @param name
	 *            nombre.
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * url.
	 * 
	 * @return url.
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * url.
	 * 
	 * @param url
	 *            url.
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	/**
	 * modulo.
	 * 
	 * @return modulo.
	 */
	public Module getModule() {
		Module module = new Module();
		Integer idModule = Converter.string2Integer(id);
		module.setId(idModule);
		module.setName(name);
		module.setUrl(url);
		return module;
	}

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		ValidateTool validateTool = new ValidateTool();
		if (validateTool.isStringEmpty(name)) {
			errors.add("name", new ActionMessage("Nombre es requerido"));
		}
		if (validateTool.isStringEmpty(url)) {
			errors.add("url", new ActionMessage("URL es requerido"));
		}
		if (errors.size() > 0) {
			request.setAttribute("error", errors);
			Module module = this.getModule();
			request.setAttribute("module", module);
		}
		return errors;
	}

}
